package com.hzky.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * Created by admin on 2017/6/7.
 */
@Controller
@RequestMapping(value = "test/upload")
public class UpLoadTest {
    @RequestMapping(value = "/page")
    String upload(Model model)
    {
        return "upLoadTest";
    }

    @RequestMapping(value = "/save")
    String upLoadSave(HttpServletRequest request, HttpServletResponse response, @RequestParam("filedata") MultipartFile file, Model model) throws IOException {
        if (!file.isEmpty()) {
            try {
                // 文件保存路径
                String filePath= URLDecoder.decode(Class.class .getResource("/").getPath(),"UTF-8") +"static/upload/"+file.getOriginalFilename();
                // 转存文件
                file.transferTo(new File(filePath));
                System.out.println(filePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "uploadSuccess";
    }
}
